<?php
/**
 * Created by PhpStorm.
 * User: hh
 * Date: 2017/5/6
 * Time: 14:56
 */

namespace system\model;

use hdphp\Model\Model;

class Category extends Model
{
    //数据表
    protected $table = "category";

    //允许填充字段
    protected $allowFill = [];

    //禁止填充字段
    protected $denyFill = [];

    //完整表名
    protected $full = false;

    //自动验证
    protected $validate = [
        //['字段名','验证方法','提示信息',验证条件,验证时间]

    ];

    //自动完成
    protected $auto = [
        //['字段名','处理方法','方法类型',验证条件,验证时机]
    ];

    //自动过滤
    protected $filter = [
        //[表单字段名,过滤条件,处理时间]
    ];

    //时间操作,需要表中存在created_at,updated_at字段
    protected $timestamps = false;


    //分类添加
    public function add(){
        /*$cname = Q('post.cname');
        $pid = Q('post.pid');
        $rows = Db::table('category')->insert(['cname'=>$cname,'pid'=>$pid]);
        if($rows){
            return true;
        }else{
            return false;
        }*/

        $category = new Category();
        $category['pid'] = Q('post.pid');
        $category['cname'] = Q('post.cname');
        $category['ctitle'] = Q('post.ctitle');
        $category['cdes'] = Q('post.cdes');
        $category['ckeywords'] = Q('post.ckeywords');
        $category['csort'] = Q('post.csort');
        return $category->save();
    }

    //子分类添加
    /*public function addSon(){
        $cname = Q('post.cname');
        $pid = Q('post.pid');
        $rows = Db::table('category')->insert(['cname'=>$cname,'pid'=>$pid]);
        if($rows){
            return true;
        }else{
            return false;
        }
    }*/

    //编辑
    public function edit(){
        $cid = Q('get.cid');
        $cname = Q('post.cname');
        $pid = Q('post.pid');
        /*p($cname);
        p($pid);*/
        $rows = Db::table('category')->where('cid',$cid)->update(['cname'=>$cname,'pid'=>$pid]);
        if($rows){
            return true;
        }else{
            return false;
        }
    }

    //递归找子集(找见自己所有的儿子)
    public function getSon($data, $cid)
    {
        static $arr = [];
        foreach ($data as $v) {
            if ($v['pid'] == $cid) {
                //证明谁是谁的儿子
                $arr[] = $v['cid'];
                //递归找子集
                $this->getSon($data, $v['cid']);
            }
        }
        return $arr;
    }

}